package com.evil.application.pojo.dto.reporting;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.evil.application.enums.form.control.ControlCodeEnum;
import com.evil.common.application.dto.order.OrderPageDataRespDTO;
import com.evil.common.core.enums.DateTypeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 报表维度
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@Accessors(chain = true)
public class ReportingHeaderDTO extends AggBaseDTO {

    /**
     * 编辑弹出窗口可见
     */
    private Boolean editPopVisible;

    /**
     * 属于表格子控件
     */
    private Boolean beingTableControl;

    /**
     * 日期格式 {@link DateTypeEnum}
     */
    private Integer dateType;

    /**
     * 后端专用
     */
    @JSONField(serialize = false)
    private String key;

    public ReportingHeaderDTO() {
        this.editPopVisible = false;
        this.beingTableControl = false;
    }

    public ReportingHeaderDTO(ReportingHeaderDTO header) {
        this.editPopVisible = header.editPopVisible;
        this.beingTableControl = header.beingTableControl;
        this.setControlId(header.getControlId());
        this.setTitle(header.getTitle());
        this.dateType = header.dateType;
        this.key = header.key;
    }

    public ReportingHeaderDTO copy() {
        ReportingHeaderDTO headerDTO = new ReportingHeaderDTO();
        headerDTO.editPopVisible = this.editPopVisible;
        headerDTO.beingTableControl = this.beingTableControl;
        headerDTO.setControlId(this.getControlId());
        headerDTO.setTitle(this.getTitle());
        headerDTO.dateType = this.dateType;
        headerDTO.key = this.key;
        return headerDTO;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ReportingHeaderDTO headerDTO = (ReportingHeaderDTO) o;
        return Objects.equals(this.getControlId(), headerDTO.getControlId()) && Objects.equals(key, headerDTO.key);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.getControlId(), key);
    }

    public List<String> getGroupKey(OrderPageDataRespDTO order) {
        return order.getData().stream()
                .filter(controlData -> controlData.getControlId().equals(this.getControlId()))
                .findFirst().map(controlData -> {
                    ControlCodeEnum controlCodeEnum = ControlCodeEnum.findByCode(controlData.getControlCode());
                    return controlData.getValues().stream()
                            .map(e -> controlCodeEnum.getDataValueToString().apply(e))
                            .collect(Collectors.toList());
                }).orElse(ListUtil.toList(""));
    }
}
